package com.dao.impl;

import com.dao.BookDao;
import com.pojo.Book;
import com.pojo.User;

import java.util.List;

public class BookDaoImp extends BaseDao implements BookDao  {
    public Integer queryForPageTotalcount(int min,int max){
        Integer in = (Integer) this.queryForSingleValue("SELECT COUNT(*) FROM t_book WHERE price BETWEEN ? AND ?",min, max);
        return in;
    }
    public List<Book> queryForPageItems(int min,int max,int begin,int pageSize){
        return this.QueryForList(Book.class,"SELECT * FROM t_book WHERE price BETWEEN ? AND ? LIMIT ?,?",min,max,begin,pageSize);


    }
    @Override
    public void addBook(Book book) {
        this.update(
                "insert into t_book values(?,?,?,?,?,?,?)",
                book.getId(),book.getName(),book.getPrice(),book.getAuthor(),book.getSales(),book.getStock(),book.getImgPath()
                );
    }

    @Override
    public void delBookById(int no) {
        this.update("delete from t_book where id=?",no);
    }

    @Override
    public void updateBook(Book book) {
            this.update("update t_book set name=?,price=?,author=?,sales=?,stock=?,img_path=? where id=?",
                    book.getName(),book.getPrice(),book.getAuthor(),book.getSales(),book.getStock(),book.getImgPath(),book.getId()
            );
    }

    @Override
    public Book queryBookById(int no) {
        return this.QueryForOne(Book.class,"select * from t_book where id=?",no);


    }

    @Override
    public List<Book> queryBooks() {
       return this.QueryForList(Book.class,"select * from t_book");
    }

    @Override
    public Integer queryForPageCount() {
//       Integer in =  (Integer) this.queryForSingleValue("select count(*) from t_book", Book.class);
//        System.out.println(in);
        return (Integer) this.queryForSingleValue("select count(*) from t_book");
    }

    @Override
    public List<Book> queryForCurrentPageData(int begin, int end) {
        return this.QueryForList(Book.class,"select * from t_book limit ?,?",begin,end);
    }
}
